package com.huawei.secure.android.common.detect;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Process;
import android.util.Log;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import com.huawei.secure.android.common.detect.a.a;
import com.huawei.secure.android.common.detect.a.b;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;

public class XposedDetect {
    private static final String TAG = "XpsDetect";
    private static final String m = "64652e726f62762e616e64726f69642e78706f7365642e696e7374616c6c6572";
    private static final String n = "64652e726f62762e616e64726f69642e78706f7365642e58706f736564427269646765";
    private static final String o = "58706f7365644272696467652e6a6172";

    public static boolean isXposedHook(Context context) {
        return a(context) && f() && g();
    }

    private static boolean a(Context context) {
        List<ApplicationInfo> installedApplications = context.getPackageManager().getInstalledApplications(DnsConfig.MAX_CACHE_ENTRIES);
        try {
            String str = new String(a.c(m), "UTF-8");
            for (ApplicationInfo applicationInfo : installedApplications) {
                if (applicationInfo.packageName.equals(str)) {
                    Log.e(TAG, "Xps apk found on the system.");
                    return true;
                }
            }
        } catch (UnsupportedEncodingException e) {
            Log.e(TAG, "UnsupportedEncodingException");
        }
        return false;
    }

    private static boolean f() {
        try {
            throw new SecurityException("Xps detect exception");
        } catch (SecurityException e) {
            for (StackTraceElement stackTraceElement : e.getStackTrace()) {
                try {
                    if (stackTraceElement.getClassName().equals(new String(a.c(n), "UTF-8")) && stackTraceElement.getMethodName().equals("main")) {
                        Log.e(TAG, "find Xps Bridge by exception class name");
                        return true;
                    }
                } catch (UnsupportedEncodingException e2) {
                    Log.e(TAG, "UnsupportedEncodingException");
                }
            }
            return false;
        }
    }

    private static boolean g() {
        InputStream fileInputStream;
        Reader inputStreamReader;
        Reader bufferedReader;
        IOException e;
        InputStream inputStream;
        Reader reader;
        Throwable th;
        InputStream inputStream2;
        Reader reader2 = null;
        try {
            String str = "/proc/" + Process.myPid() + "/maps";
            CharSequence str2 = new String(a.c(o), "UTF-8");
            fileInputStream = new FileInputStream(str);
            try {
                inputStreamReader = new InputStreamReader(fileInputStream, Charset.forName("UTF-8"));
                try {
                    bufferedReader = new BufferedReader(inputStreamReader);
                    do {
                        try {
                            str = bufferedReader.readLine();
                            if (str == null) {
                                b.a(bufferedReader);
                                b.a(inputStreamReader);
                                b.a(fileInputStream);
                                return false;
                            }
                        } catch (IOException e2) {
                            e = e2;
                            inputStream = fileInputStream;
                            reader = bufferedReader;
                        } catch (Throwable th2) {
                            th = th2;
                            reader2 = inputStreamReader;
                        }
                    } while (!str.contains(str2));
                    Log.e(TAG, "Xps JAR found: ");
                    b.a(bufferedReader);
                    b.a(inputStreamReader);
                    b.a(fileInputStream);
                    return true;
                } catch (IOException e3) {
                    e = e3;
                    inputStream2 = fileInputStream;
                    reader = null;
                    inputStream = inputStream2;
                    try {
                        Log.e(TAG, "Xps Jar Check " + e.getMessage());
                        b.a(reader);
                        b.a(inputStreamReader);
                        b.a(inputStream);
                        return false;
                    } catch (Throwable th3) {
                        th = th3;
                        bufferedReader = reader;
                        fileInputStream = inputStream;
                        reader2 = inputStreamReader;
                        b.a(bufferedReader);
                        b.a(reader2);
                        b.a(fileInputStream);
                        throw th;
                    }
                } catch (Throwable th4) {
                    th = th4;
                    bufferedReader = null;
                    reader2 = inputStreamReader;
                    b.a(bufferedReader);
                    b.a(reader2);
                    b.a(fileInputStream);
                    throw th;
                }
            } catch (IOException e4) {
                e = e4;
                inputStreamReader = null;
                inputStream2 = fileInputStream;
                reader = null;
                inputStream = inputStream2;
                Log.e(TAG, "Xps Jar Check " + e.getMessage());
                b.a(reader);
                b.a(inputStreamReader);
                b.a(inputStream);
                return false;
            } catch (Throwable th5) {
                th = th5;
                bufferedReader = null;
                b.a(bufferedReader);
                b.a(reader2);
                b.a(fileInputStream);
                throw th;
            }
        } catch (IOException e5) {
            e = e5;
            inputStreamReader = null;
            reader = null;
            Log.e(TAG, "Xps Jar Check " + e.getMessage());
            b.a(reader);
            b.a(inputStreamReader);
            b.a(inputStream);
            return false;
        } catch (Throwable th6) {
            th = th6;
            fileInputStream = null;
            bufferedReader = null;
            b.a(bufferedReader);
            b.a(reader2);
            b.a(fileInputStream);
            throw th;
        }
    }
}
